//
//  PlatformWallet.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/20.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "PlatformWallet.h"
#import "MenuView.h"
#import "PlatformWalletCell.h"
#import "PlatformWalletBill.h"
#define fDeviceWidth ([UIScreen mainScreen].bounds.size.width)
#define fDeviceHeight ([UIScreen mainScreen].bounds.size.height-StatusBarHeight)
@interface PlatformWallet ()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDelegate> {
    UIView *_headView;
    NSMutableArray *dataSource; //*<数据源*/
}
@property(nonatomic,retain)MenuView *menuView;
@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation PlatformWallet

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"平台钱包";
    [self setRightBarButtonImage:LOADIMAGE(@"btn_more") highlightedImage:nil selector:@selector(menu)];
    
    [self allocHeadView];
    [self allocCollectionView];
    
    dataSource = [[NSMutableArray alloc] initWithObjects:@"账单",@"银行", nil];
    
}
#pragma mark --初始化_headView --
-(void)allocHeadView {
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 120)];
    _headView.backgroundColor = [UIColor grayColor];
    NSArray *number = @[@"￥1000",@"￥2000",@"￥0"];
    NSArray *title = @[@"可用金额",@"交易中金额",@"冻结金额"];
    for (int i = 0; i<3; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(CGRectGetWidth(_headView.frame)/3*i, CGRectGetMinY(_headView.frame), CGRectGetWidth(_headView.frame)/3, CGRectGetHeight(_headView.frame));
        [btn setTitle:[NSString stringWithFormat:@"%@\n%@",number[i],title[i]] forState:UIControlStateNormal];
        btn.titleLabel.numberOfLines = 0;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn addTarget:self action:@selector(BtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headView addSubview:btn];
    }
}
#pragma mark --Btn点击事件--
-(void)BtnAction:(UIButton *)sender {
    [self showText:@"按钮也有尊严,请不要随便点我!"];
}
#pragma mark --初始化collectionView--
-(void)allocCollectionView {
    float AD_height = 120;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, AD_height);//头部
    flowLayout.minimumInteritemSpacing = 0.0;
    flowLayout.minimumLineSpacing = 0.0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:self.collectionView];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //注册cell和ReusableView（相当于头部）
    [self.collectionView registerClass:[PlatformWalletCell class] forCellWithReuseIdentifier:@"cell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView"];
}
-(void)menu{
    NSArray * arr = nil;
    arr = @[@"支付管理", @"支付安全", @"常见问题"];
    _menuView = [[MenuView alloc] initWithButtonTitles:arr withDelegate:self];
    _menuView.hasImage = NO;
    [_menuView showInView:self.view origin:CGPointMake(self.view.width - 180, 70)];
    _menuView.buttonView.delegate=self;
}

#pragma mark -
#pragma mark --UIcolletionViewDelegate&dataSource--
//定义展示的UIcolletionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 9;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
//每个UIcolletionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identify = @"cell";
    PlatformWalletCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    [cell sizeToFit];
    if (!cell) {
        DLog(@"无法创建CollectionViewCell时打印，自定义的cell就不可能进来了。");
    }
    if (dataSource.count > indexPath.row) {
        cell.text.text = dataSource[indexPath.row];
    }
    
    return cell;
}
//头部显示的内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"ReusableView" forIndexPath:indexPath];
    
    [headerView addSubview:_headView];//头部广告栏
    return headerView;
    
}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //边距占5*4=20 ，2个
    //图片为正方形，边长：(fDeviceWidth-20)/2-5-5 所以总高(fDeviceWidth-20)/2-5-5 +20+30+5+5 label高20 btn高30 边
    return CGSizeMake((fDeviceWidth)/3, (fDeviceWidth)/3);
}
//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
//定义每个UICollectionView 纵向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    //    cell.backgroundColor = [UIColor redColor];
    switch (indexPath.row) {
        case 0:
        {
            [self pushViewController:[PlatformWalletBill new]];
            break;
        }
            
            
        default:
            break;
    }
    DLog(@"选择%ld",(long)indexPath.row);
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
